﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using System.Drawing;

namespace WebApplication1
{
    public partial class AddPrefectBatch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

       

        protected void btnPrefectCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnPrefectSubmit_Click(object sender, EventArgs e)
        {
            DateTime prefectBatchFrm = DateTime.MinValue;
            DateTime prefectBatchTo = DateTime.MinValue;

             DateTime prefectBatchFrmDte = DateTime.MinValue;
            DateTime prefectBatchToDte = DateTime.MinValue;

            int prefectFrmYr = 0;
            int prefectToYr = 0;

            bool convertableToDatePrfctBtchFrm = DateTime.TryParse(txtPrefctBatchFrmDate.Text, out prefectBatchFrm);
            bool convertableToDatePrfctBtchTo = DateTime.TryParse(txtPrefctBatchToDate.Text, out prefectBatchTo);

            if (convertableToDatePrfctBtchFrm)
            {
                prefectBatchFrmDte = prefectBatchFrm; 
            }
            else
            {
                bool convertableToInt = Int32.TryParse(txtPrefctBatchFrmDate.Text, out prefectFrmYr);
                //Year should be yyyy format
                if (convertableToInt && txtPrefctBatchFrmDate.Text.Length == 4)
                {
                    prefectBatchFrmDte = Convert.ToDateTime(txtPrefctBatchFrmDate.Text + "-07-01");
                }
                else
                {
                    // Response.Redirect("AddPrefectBatch.aspx?Entered Prefect's Batch From Year is not in valid format");
                    lblAddPrefectBatch.Text = "Entered Prefect's Batch From Year is not in valid format";
                    return;
                }
            }

            if (convertableToDatePrfctBtchTo)
            {
                prefectBatchToDte = prefectBatchTo; 
            }
            else
            {
                bool convertableToInt = Int32.TryParse(txtPrefctBatchToDate.Text, out prefectToYr);
                //Year should be yyyy format
                if (convertableToInt && txtPrefctBatchFrmDate.Text.Length == 4)
                {
                    prefectBatchToDte = Convert.ToDateTime(txtPrefctBatchToDate.Text + "-06-30");
                }
                else
                {
                    //Response.Redirect("AddPrefectBatch.aspx?Entered Prefect's Batch To Year is not in valid format");
                    lblAddPrefectBatch.Text = "Entered Prefect's Batch To Year is not in valid format";
                    return;
                }
            }

            try
            {
                if ((prefectBatchToDte.Year - prefectBatchFrmDte.Year) > 5 )
                {

                    // Response.Redirect("AddPrefectBatch.aspx?Entered Committee Start Year & End Year are too differ");
                    lblAddPrefectBatch.Text = "Entered Prefect's Start Year & End Year are too differ ";
                    return;

                }
                else if ((prefectBatchToDte.Year - prefectBatchFrmDte.Year) < 0)
                {
                    lblAddPrefectBatch.Text = "Entered Prefect's end date is less than start year";
                    return;
                }

                
                else
                {
                    if ((prefectBatchToDte.Year - prefectBatchFrmDte.Year) == 0)
                    {
                        prefectBatchFrmDte = Convert.ToDateTime(prefectBatchFrmDte.Year + "-01-01");
                        prefectBatchToDte = Convert.ToDateTime(prefectBatchToDte.Year + "-12-31");
                    }

                    PrefectBatchDTO prefectBatchDTO = new PrefectBatchDTO
                    {
                        PrefectBatchId = Guid.NewGuid(),
                        PrefectBatchName = "J/Hartley College's Prefect's Batch  for the Year of  " + prefectBatchToDte.Year + "/" + prefectBatchFrmDte.Year,
                        PrefectFrom = prefectBatchFrmDte,
                        PrefectTo = prefectBatchToDte,
                        Description = txtPrefectBatchDesc.Text,
                        PrefectBatchPhotoName = fuPrefectPhoto.FileName,
                        PrefectBatchPhotoPath = Server.MapPath("PrefectBatchPhotos") + "\\" + fuPrefectPhoto.FileName,
                        // ModifiedBy ='',
                        ModifiedTime = DateTime.Now
                    };
                    StudentAL studentAL = new StudentAL();

                    string result = studentAL.SavePrefectBatch(prefectBatchDTO);
                    if (result == "Saved")
                    {
                        if ((fuPrefectPhoto.PostedFile != null) && (fuPrefectPhoto.PostedFile.ContentLength > 0))
                        {
                            string fileName = System.IO.Path.GetFileName(fuPrefectPhoto.PostedFile.FileName);
                            string saveLocation = Server.MapPath("PrefectBatchPhotos") + "\\" + fileName;
                            try
                            {
                                fuPrefectPhoto.PostedFile.SaveAs(saveLocation);
                                Response.Write("The File has been uploaded successfully");
                            }
                            catch (Exception ex)
                            {
                                Response.Write("Error occured while uploading the files...!!!" + ex);
                            }
                        }
                        else
                        {
                            Response.Write("Please select a file to upload.");

                        }

                        lblAddPrefectBatch.Text = "The Prefect Batch details have been saved successfully";
                    }

                    else if (result == "Updated")
                    {
                        lblAddPrefectBatch.Text = "The Prefect Batch details have been Updated successfully";
                    }

                    else if (result == "AlreadyExists")
                    {
                        lblAddPrefectBatch.Text = "The Prefect Batch details added by you is already exists with this name or there is aleady a Prefect batch has been entered with same start & end year";
                    }
                    else
                    {
                        lblAddPrefectBatch.Text = "Due to some errors  your data is not added to our web site";
                    }
                }
            }
            catch (Exception ee)
            {
                lblAddPrefectBatch.Text = "There are some exceptions while adding prefect batch details." + ee.Message;
            }
        
        }
    }
}